#include "threadPool.h"
int token_genarate(char* username,char* token)
{
    char* jwt;
    size_t jwt_length;

    struct l8w8jwt_encoding_params params;
    l8w8jwt_encoding_params_init(&params);

    params.alg = L8W8JWT_ALG_HS512;
    params.sub = "Netdisk";
    params.sub_length = strlen("Netdisk");

    params.iss = "server token";
    params.iss_length = strlen("server token");

    params.aud = "client";
    params.aud_length = strlen("client");

    params.iat = 0;
    params.exp = 0x7FFFFFFF;

    params.secret_key = (unsigned char*)username;
    params.secret_key_length = strlen(username);
    params.out = &jwt;
    params.out_length = &jwt_length;

    int r = l8w8jwt_encode(&params);
    if(r == L8W8JWT_SUCCESS)
    {
        strcpy(token,jwt);
    }
    else
    {
        free(jwt);
        return -1;
    }

    free(jwt);

    return 0;
}

int token_check(char* username,char* token)
{
    struct l8w8jwt_decoding_params params;
    l8w8jwt_decoding_params_init(&params);

    params.alg = L8W8JWT_ALG_HS512;

    params.jwt = token;
    params.jwt_length = strlen(token);

    params.verification_key = (unsigned char*)username;
    params.verification_key_length = strlen(username);

    params.validate_sub = "Netdisk";
    params.validate_sub_length = strlen(params.validate_sub);

    params.validate_iss = "server token";
    params.validate_iss_length = strlen(params.validate_iss);

    params.validate_aud = "client";
    params.validate_aud_length = strlen(params.validate_aud);

    params.validate_iat = 0;
    params.validate_exp = 0x7FFFFFFF;

    enum l8w8jwt_validation_result validation_result;

    int decode_result = l8w8jwt_decode(&params, &validation_result, NULL, NULL);
    if (decode_result == L8W8JWT_SUCCESS && validation_result == L8W8JWT_VALID)
    {
        return 0;
    }
    else
    {
        return -1;
    }

    return 0;
}

